﻿function ExportToExcel(tableid, filename) {
    var table = document.getElementById(tableid);
    if (!table) { return; }

    var data = getTableData(table);
    var objBody = document.getElementsByTagName("body").item(0);
    var objExpTemp = document.getElementById('objExpTemp');

    if (!objExpTemp) {
        objExpTemp = document.createElement("iframe");
        objExpTemp.setAttribute('id', 'objExpTemp');
        objExpTemp.style.display = 'none';
        objExpTemp.src = 'about:blank';
        objBody.appendChild(objExpTemp);
    }

    var myForm = objExpTemp.contentWindow.document.forms['myForm'];
    if (!myForm) {
        objExpTemp.contentWindow.document.write('<div style="display:none"><form name="myForm"><input id="expContent" name="content" type="text" /><input id="expFileName" name="fileName" type="text" /></form></div>');
    }
    var myForm = objExpTemp.contentWindow.document.forms['myForm'];
    var txtData = objExpTemp.contentWindow.document.getElementById('expContent');
    txtData.value = data;
    var txtFileName = objExpTemp.contentWindow.document.getElementById('expFileName');
    txtFileName.value = filename;
    
    myForm.action = 'ExcelTransfer.aspx';
    myForm.method = 'POST';
    myForm.submit();

    return;
}

function getTableData(table) {
    var tableData = "<table border=\"1\">" + table.innerHTML + "</table>";
    return tableData;
}